export const setNotificationChannel = (channelList) => {
  // #ifdef APP-PLUS
  if (plus.os.name === 'Android') {
    // 判断是Android
    plus.android.importClass('android.app.NotificationManager')
    plus.android.importClass('android.app.NotificationChannel')
    const main: any = plus.android.runtimeMainActivity()
    const notificationManager = main.getSystemService(main.NOTIFICATION_SERVICE)
    // 创建通知渠道
    const NotificationChannel: any = plus.android.importClass('android.app.NotificationChannel')
    channelList.forEach((channel) => {
      const notificationChannel = new NotificationChannel(
        channel.channelId,
        channel.channelName,
        notificationManager.IMPORTANCE_HIGH,
      )
      notificationChannel.setDescription(channel.description)
      notificationManager.createNotificationChannel(notificationChannel)
      console.info('创建推送频道=================>' + notificationChannel)
      // 设置通知频道状态
      setNotificationChannelStatus(channel.channelId, channel.enabled)
    })
  }
  // #endif
}

export function setNotificationChannelStatus(channelId, enabled) {
  plus.android.importClass('android.app.NotificationManager')
  plus.android.importClass('android.content.Context')
  plus.android.importClass('android.app.NotificationChannel')

  const mainActivity: any = plus.android.runtimeMainActivity()
  const main: any = plus.android.runtimeMainActivity()
  const context: any = mainActivity.getApplicationContext()

  const notificationManager = main.getSystemService(main.NOTIFICATION_SERVICE)

  const notificationChannel = plus.android.invoke(
    notificationManager,
    'getNotificationChannel',
    channelId,
  )
  if (notificationChannel) {
    if (!enabled) {
      plus.android.invoke(notificationChannel, 'setImportance', notificationManager.IMPORTANCE_NONE)
      plus.android.invoke(notificationManager, 'deleteNotificationChannel', channelId)
      console.info(
        '删除通知频道状态=================>' + enabled + '==============>' + notificationChannel,
      )
    } else {
      plus.android.invoke(notificationChannel, 'setImportance', notificationManager.IMPORTANCE_HIGH)
      plus.android.invoke(notificationManager, 'createNotificationChannel', notificationChannel)
      console.info(
        '设置通知频道状态=================>' + enabled + '==============>' + notificationChannel,
      )
    }
    //    const importance = enabled
    //      ? notificationManager.IMPORTANCE_HIGH
    //      : notificationManager.IMPORTANCE_NONE
    //    plus.android.invoke(notificationChannel, 'setImportance', importance)
    //    plus.android.invoke(notificationManager, 'createNotificationChannel', notificationChannel)
    // console.info(
    //   '设置通知频道状态=================>' +
    //     enabled +
    //     '========>' +
    //     importance +
    //     '==============>' +
    //     notificationChannel,
    // )
  }
}

export function getNotificationChannelStatus(channelId) {
  // #ifdef APP-PLUS
  plus.android.importClass('android.app.NotificationManager')
  plus.android.importClass('android.content.Context')

  const mainActivity: any = plus.android.runtimeMainActivity()
  const context: any = mainActivity.getApplicationContext()

  const notificationManager = mainActivity.getSystemService(mainActivity.NOTIFICATION_SERVICE)

  const channel = plus.android.invoke(notificationManager, 'getNotificationChannel', channelId)

  console.info('获取频道1=================>' + channel + channelId)

  if (channel) {
    console.info('获取频道2=================>' + channel)
    const importance = plus.android.invoke(channel, 'getImportance')
    return importance !== 0 // 0 表示关闭
  }
  return false
  // #endif
}
